package com.huahua.api.api;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.huahua.api.base.QiNiuOssUploadBean;
import com.huahua.api.entity.dto.novel.SourceDto;
import com.huahua.api.entity.pojo.*;
import com.huahua.api.processor.NovelPostProcessorExecutor;
import com.huahua.api.service.*;
import com.huahua.api.util.QiNiuUtils;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author LoveHuahua
 * @date 2021年09月07日 19:46
 * @description believe in yourself
 * 小说采集资源站
 */
@Component
@Slf4j
public class NovelReaderApi {


    private static final String api = "https://www.xbiquge.la";
    @Autowired
    ApiNovelTypeService typeService;
    @Autowired
    ApiNovelBookContentService contentService;
    @Autowired
    ApplicationContext applicationContext;
    @Autowired
    QiNiuUtils qiNiuUtils;
    @Autowired
    ApiNovelBookNotFoundService novelBookNotFoundService;
    @Autowired
    NovelPostProcessorExecutor processorExecutor;
    @Autowired
    ApiNovelBookViewCountService countService;
    @Autowired
    SyncBookService syncBookService;
    @Autowired
    private ApiNovelBookService bookService;

//    public static void main(String[] args) throws IOException {
//
//    }

    /**
     * 解析 https://www.xbiquge.la/fenlei/5_0.html 可以解释为全部小说
     *
     * @param document 网页内容
     */
    private static ArrayList<ApiNovelBook> parseCategoryPage(Document document) {
        Elements elements = document.select("#newscontent > div.l > ul > li");
        ArrayList<ApiNovelBook> books = Lists.newArrayList();
        //每一本小说
        for (Element element : elements) {
            ApiNovelBook book = new ApiNovelBook();
            book.setAuthor(element.select("span.s5").text());
            book.setTitle(element.select("span.s2").text().replaceAll("《", "").replaceAll("》", ""));
            String href = element.select("span.s2 > a").attr("href");
            SourceDto sourceDto = parseSource(href);
            book.setSourceId(sourceDto.getSourceId());
            book.setSourceType(sourceDto.getSourceType());
            book.setReadUrl(href);
            books.add(book);
        }
        return books;
    }

    private static SourceDto parseSource(String readUrl) {
        String[] split = readUrl.replaceAll("https://www.xbiquge.la/", "").split("/");
        SourceDto sourceDto = new SourceDto();
        sourceDto.setSourceId(Integer.parseInt(split[1]));
        sourceDto.setSourceType(Integer.parseInt(split[0]));

        return sourceDto;
    }

    /**
     * 解析文章内容 之前这里是设置代理 后面不稳定去除了
     * <p>
     * IpProxyDto ipProxyDto = new ObjectMapper().readValue(Jsoup.connect("http://demo.spiderpy.cn/get/").ignoreContentType(true).ignoreHttpErrors(true).get().text(), IpProxyDto.class);
     * String proxy = ipProxyDto.getProxy();
     * String[] split = proxy.split(":");
     * String host = split[0];
     * String port = split[1];
     *
     * @param href
     * @throws IOException
     */
    private void parseContent(String href) throws IOException {

        //章节完整来源
        Document document = Jsoup.connect(href).timeout(30000).get();
        //文章内容
        String text = document.select("#content").text().replaceAll(" ", "\n");
        ApiNovelBookContent one = contentService.lambdaQuery().select(ApiNovelBookContent::getId).eq(ApiNovelBookContent::getContentUrl, href).one();
        ApiNovelBookContent apiNovelBookContent = new ApiNovelBookContent();
        apiNovelBookContent.setContent(text);
        apiNovelBookContent.setId(one.getId());
        processorExecutor.execute(apiNovelBookContent);
        contentService.updateById(apiNovelBookContent);
    }

    /**
     * 2021-09-16 新增字段 需要填充
     */
    public void fixBookImg() throws IOException {
        List<ApiNovelBook> list;
        do {
            list = bookService.lambdaQuery().isNull(ApiNovelBook::getImgUrl).last("limit 100").list();
            for (ApiNovelBook apiNovelBook : list) {
                try {
                    String url = "https://www.xbiquge.la/files/article/image/" + apiNovelBook.getSourceType() + "/" + apiNovelBook.getSourceId() + "/" + apiNovelBook.getSourceId() + "s.jpg";
                    System.out.println("图片url" + url);
                    BufferedInputStream bufferedInputStream = Jsoup.connect(url).method(Connection.Method.GET).ignoreHttpErrors(true).ignoreContentType(true).execute().bodyStream();
                    QiNiuOssUploadBean qiNiuOssUploadBean = qiNiuUtils.uploadFileByStream(bufferedInputStream, "jpg");
                    apiNovelBook.setImgUrl(qiNiuOssUploadBean.getUrl());
                    System.out.println(apiNovelBook.getImgUrl());
                    bookService.updateById(apiNovelBook);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } while (!list.isEmpty());
    }

    /**
     * 2021-09-18 修复部分字段 source_id = 0
     *
     * @throws IOException
     */
    public void fixSource() throws IOException {
        List<ApiNovelBook> list;
        do {
            list = bookService.lambdaQuery().eq(ApiNovelBook::getSourceId, 0).last("limit 100").list();
            for (ApiNovelBook apiNovelBook : list) {
                String readUrl = apiNovelBook.getReadUrl();
                String[] split = readUrl.replaceAll("https://www.xbiquge.la/", "").split("/");
                bookService.lambdaUpdate().set(ApiNovelBook::getSourceId, split[1]).set(ApiNovelBook::getSourceType, split[0]).update();
            }
        } while (!list.isEmpty());
    }

    /**
     * 同步书架
     *
     * <p>
     * 已废弃版本的随机book如下
     * for (int i = 0; i < 100; i++) {
     * List<ApiNovelBook> list = bookService.lambdaQuery().select(ApiNovelBook::getSourceId).eq(ApiNovelBook::getSourceType, i).list();
     * List<ApiNovelBookNotFound> notFoundList = novelBookNotFoundService.lambdaQuery().select(ApiNovelBookNotFound::getContentUrl).list();
     * Map<String, ApiNovelBook> maps = Maps.uniqueIndex(list, input -> String.valueOf(input.getSourceId()));
     * ImmutableMap<String, ApiNovelBookNotFound> stringApiNovelBookNotFoundImmutableMap = Maps.uniqueIndex(notFoundList, ApiNovelBookNotFound::getContentUrl);
     * //默认取50000
     * for (int j = 1; j < 50000; j++) {
     * String url = api + "/" + i + "/" + j + "/";
     * if (maps.containsKey(String.valueOf(j))) {
     * continue;
     * }
     * //404的地址
     * if (stringApiNovelBookNotFoundImmutableMap.containsKey(url)) {
     * continue;
     * }
     * Thread.sleep(1000 * 10);
     * try {
     * //同步书籍名称
     * syncMyBook(url);
     * } catch (HttpStatusException e) {
     * if (e.getStatusCode() == 404) {
     * ApiNovelBookNotFound entity = new ApiNovelBookNotFound();
     * entity.setContentUrl(url);
     * novelBookNotFoundService.save(entity);
     * }
     * } catch (Exception e) {
     * e.printStackTrace();
     * }
     * }
     * }
     */
    public void syncBookStores() throws IOException {
        for (int i = 1; i < 8; i++) {
            List<ApiNovelBook> list = bookService.lambdaQuery().select(ApiNovelBook::getReadUrl).list();
            Map<String, ApiNovelBook> maps = Maps.uniqueIndex(list, ApiNovelBook::getReadUrl);
            HashMap<String, ApiNovelBook> stringApiNovelBookHashMap = Maps.newHashMap(maps);
            Document firstDocument = Jsoup.connect("https://www.xbiquge.la/fenlei/" + i + "_0.html").get();
            Elements select = firstDocument.select("#pagelink > a.last");
            int page = Integer.parseInt(select.text());
            //解析当前
            ArrayList<ApiNovelBook> firstBooks = parseCategoryPage(firstDocument).stream().filter(book -> !stringApiNovelBookHashMap.containsKey(book.getReadUrl())).peek(book -> stringApiNovelBookHashMap.put(book.getReadUrl(), book)).collect(Collectors.toCollection(ArrayList::new));
            bookService.saveBatch(firstBooks);

            //遍历解析之后的每一个
            for (int j = 1; j < page; j++) {
                Document document = Jsoup.connect("https://www.xbiquge.la/fenlei/" + i + "_" + j + ".html").get();
                ArrayList<ApiNovelBook> collect = parseCategoryPage(document).stream().filter(book -> !stringApiNovelBookHashMap.containsKey(book.getReadUrl())).peek(book -> stringApiNovelBookHashMap.put(book.getReadUrl(), book)).collect(Collectors.toCollection(ArrayList::new));
                bookService.saveBatch(collect);
            }
        }
    }

    public void syncContent() {
        List<ApiNovelBookContent> list;
        while ((list = contentService.lambdaQuery().isNull(ApiNovelBookContent::getContent).last("limit 100").list()).size() != 0) {
            for (ApiNovelBookContent bookContent : list) {
                try {
                    //先延迟在获取，否则获取报错不会堵塞
                    Thread.sleep(1000 * 10);
                    parseContent(bookContent.getContentUrl());
                } catch (IOException | InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 从书架获取不会有详细信息，只有作者，标题，地址，这里需要重新同步一遍基础信息
     */
    public void syncBookDesc() throws IOException, InterruptedException {
        List<ApiNovelBook> list;
        while (!(list = bookService.lambdaQuery().isNull(ApiNovelBook::getDesc).last("limit 100").list()).isEmpty()) {
            for (ApiNovelBook book : list) {
                syncMyBook(book.getReadUrl());
                Thread.sleep(1000 * 10);
            }
        }

    }

    /**
     * "https://www.xbiquge.la/0/951/"
     *
     * @param url
     * @throws IOException
     */
    @Async
    public void syncMyBook(String url) throws IOException, InterruptedException {
        Document document = Jsoup.connect(url).get();
        String title = document.select("html > head > meta[property=og:novel:book_name]").attr("content");
        String desc = document.select("html > head > meta[property=og:description]").attr("content");
        String type = document.select("html > head > meta[property=og:novel:category]").attr("content");
        String author = document.select("html > head > meta[property=og:novel:author]").attr("content");
        String readUrl = document.select("html > head > meta[property=og:novel:read_url]").attr("content");
        String updateTime = document.select("#info > p:nth-child(4)").text();
        log.info("标题: {}", title);
        log.info("描述: {}", desc);
        log.info("类别: {}", type);
        log.info("作者: {}", author);
        log.info("书籍主页: {}", readUrl);
        updateTime = updateTime.replaceAll("最后更新：", "");
        LocalDateTime parse = LocalDateTime.parse(updateTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        log.info("最后更新时间: {}", parse);


        ApiNovelBook apiNovelBook = new ApiNovelBook();
        apiNovelBook.setAuthor(author);
        apiNovelBook.setDesc(desc);
        apiNovelBook.setTitle(title);
        apiNovelBook.setReadUrl(readUrl);

        String[] split = url.replaceAll("https://www.xbiquge.la/", "").split("/");
        // FIXME: 2021/9/18 修复source字段取值问题
        apiNovelBook.setSourceType(Integer.valueOf(split[0]));
        apiNovelBook.setSourceId(Integer.valueOf(split[1]));


        //设置类别
        Long count = typeService.lambdaQuery().eq(ApiNovelType::getName, type).count();
        if (count == 0) {
            ApiNovelType apiNovelType = new ApiNovelType();
            apiNovelType.setName(type);
            typeService.save(apiNovelType);
        }
        ApiNovelType one = typeService.lambdaQuery().eq(ApiNovelType::getName, type).one();
        apiNovelBook.setTypeId(one.getId());
        //更新数据状态
        ApiNovelBook apiNovelBook1 = bookService.lambdaQuery().eq(ApiNovelBook::getReadUrl, readUrl).one();
        if (apiNovelBook1 == null) {
            //缩略图
            setImgUrl(apiNovelBook);
            bookService.save(apiNovelBook);
        } else {
            if (StringUtils.isEmpty(apiNovelBook1.getImgUrl())) {
                setImgUrl(apiNovelBook);
            }
            Integer id = apiNovelBook1.getId();
            apiNovelBook.setId(id);
            bookService.updateById(apiNovelBook);
        }
        List<ApiNovelBookContent> list = contentService.lambdaQuery().eq(ApiNovelBookContent::getBookId, apiNovelBook.getId()).list();
        HashMap<String, ApiNovelBookContent> map = new HashMap<>();
        for (ApiNovelBookContent apiNovelBookContent : list) {
            map.put(apiNovelBookContent.getContentUrl(), apiNovelBookContent);
        }

        //爬取章节(所有章节数)
        for (Element element : document.select("div.box_con > div#list > dl >  dd ")) {
            //地址
            String href = element.select("a").attr("href");
            String contentUrl = api + href;
            //判断是否解析过
            if (map.get(contentUrl) == null) {
                //章节名
                String name = element.select("a").text();
                //原本这里是解析小说内容的,现在改成保存小说了
                ApiNovelBookContent apiNovelBookContent = new ApiNovelBookContent();
                apiNovelBookContent.setBookId(apiNovelBook.getId());
                apiNovelBookContent.setContentUrl(contentUrl);
                apiNovelBookContent.setContentTitle(name);
                ApiNovelBookContent bookContent = contentService.lambdaQuery().eq(ApiNovelBookContent::getContentUrl, contentUrl).one();
                if (bookContent == null) {
                    contentService.save(apiNovelBookContent);
                }
            }
        }
        Thread.sleep(1000 * 10);
    }

    private void setImgUrl(ApiNovelBook apiNovelBook) throws IOException {
        //缩略图
        String imgUrl = "https://www.xbiquge.la/files/article/image/" + apiNovelBook.getSourceType() + "/" + apiNovelBook.getSourceId() + "/" + apiNovelBook.getSourceId() + "s.jpg";
        BufferedInputStream bufferedInputStream = Jsoup.connect(imgUrl).method(Connection.Method.GET).ignoreHttpErrors(true).ignoreContentType(true).execute().bodyStream();
        QiNiuOssUploadBean qiNiuOssUploadBean = qiNiuUtils.uploadFileByStream(bufferedInputStream, "jpg");
        //为空 上传失败
        if (qiNiuOssUploadBean == null){
            //填充默认图片
            apiNovelBook.setImgUrl("http://cdn3.jxwazx.cn/1631800656748e48.jpg");
            log.info("上传失败: {}", imgUrl);
            return;
        }
        apiNovelBook.setImgUrl(qiNiuOssUploadBean.getUrl());
        log.info("缩略图地址: {}", apiNovelBook.getImgUrl());
    }

    /**
     * 同步已有的书籍列表
     * 原因:只依靠书架同步的话,实时性太差
     * 目前设想是
     * 1.通过小说点击量来优先选择同步
     * 2.0章节的优先同步
     */
    public void syncHasBook() throws IOException, InterruptedException {
        //实现2:0章节的优先同步
        List<SyncBook> syncBooks = syncBookService.lambdaQuery().eq(SyncBook::getCount, 0).list();
        for (SyncBook syncBook : syncBooks) {
            syncMyBook(syncBook.getReadUrl());
        }

        //实现1 通过小说点击量来优先选择同步
        List<ApiNovelBookViewCount> list = countService.lambdaQuery().orderByDesc(ApiNovelBookViewCount::getViewCount).list();
        for (ApiNovelBookViewCount apiNovelBookViewCount : list) {
            ApiNovelBook one = bookService.lambdaQuery().eq(ApiNovelBook::getId, apiNovelBookViewCount.getBookId()).one();
            syncMyBook(one.getReadUrl());
        }


    }
}
